<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Part;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\PartCategory;
use App\Models\Device;
use App\Models\Manufacturer;
use App\Admin\Actions\Grid\OwningDevice;
use App\Admin\Actions\Grid\ScrapEquipment;

class PartController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Part(), function (Grid $grid) {
            $grid->tableCollapse(false);
            $grid->column('id')->sortable();

          
           
            $grid->column('asset_number')->display(function ($asset_number) {
                return '<span style="background-color: pink; color: black;">' . $asset_number . '</span>';
            return $asset_number;
            });

          

            $grid->column('Parts_classification')->display(function ($PartCategoryId) {
                $PartCategory = PartCategory::find($PartCategoryId);
                return $PartCategory ? $PartCategory->PartCategory : '';
            });

            $grid->column('Owning_device')->display(function ($Asset_NumberId) {
                $Asset_Number = Device::find($Asset_NumberId);
                return $Asset_Number ? $Asset_Number->Asset_Number : '';
            });
            
           
            
            $grid->column('name');
            $grid->column('Part_serial_number');
            $grid->column('specification');
            $grid->column('manufacturer_id')->display(function ($manufacturer) {
               // return $this->manufacturerRelation->manufacturer ?? '';
                return Manufacturer::find($manufacturer)->manufacturer;
            });

            
            $grid->column('state')->display(function ($state) {
                if ($state === '正常') {
                    return '<span style="background-color: green; color: white;">' . $state . '</span>';
                }
                if ($state === '空闲') {
                    return '<span style="background-color: black; color: white;">' . $state . '</span>';
                }
                if ($state === '报废') {
                    return '<span style="background-color: red; color: white;">' . $state . '</span>';
                }
                return $state;
            });
            $grid->column('remark')->textarea();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
        
                
                $filter->like('name')->width(2);
                $filter->equal('Parts_classification')->select(PartCategory::pluck('PartCategory', 'id'))->width(2);
                $filter->equal('Owning_device')->select(Device::pluck('Asset_Number', 'id'))->width(2);
                $filter->like('specification')->width(2);
                //$filter->equal('User')->select(Administrator::pluck('name', 'id'))->width(1);
                //$filter->like('state')->width(1);
        
            });
                //开启弹窗创建表单
                $grid->enableDialogCreate();
                // 显示快捷编辑按钮
                $grid->showQuickEditButton();
                // 禁用编辑按钮
                $grid->disableEditButton(); 
                //设置列选择器
                $grid->showColumnSelector();
                $grid->hideColumns(['Part_serial_number','specification','remark','updated_at','id',]);
                $grid->actions([new OwningDevice()]);
                $grid->actions([new ScrapEquipment()]);


                
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Part(), function (Show $show) {
            $show->field('id');
            $show->field('asset_number');
            $show->field('Parts_classification');
            $show->field('Owning_device');
            $show->field('name');
            $show->field('Part_serial_number');
            $show->field('specification');
            $show->field('manufacturer_id');
            $show->field('state');
            $show->field('remark');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Part(), function (Form $form) {
            $form->display('id');
            $hour = 10;
            $hourFormatted = sprintf('%02d', $hour);
            $assetNumber = 'FMSRC-' . date('Ymd') . '-' . $hourFormatted . date('is');
            $form->text('asset_number')->default($assetNumber)->required();
            $form->select('Parts_classification', '分类')->required()->options(function () {
                return PartCategory::pluck('PartCategory', 'id');
            });
            $form->select('Owning_device', '所属设备')->options(function () {
                 return Device::selectRaw("CONCAT(Asset_Number, ' - ', IFNULL(User_id, '未分配用户')) AS value, id")
                  ->pluck('value', 'id');
            });
            $form->text('name')->required();
            //$form->text('Part_serial_number');
            $form->text('specification');
          

            $form->select('manufacturer_id', '品牌')->required()->options(function () {
                return Manufacturer::pluck('manufacturer', 'id');
            });
            $form->select('state', '状态')->options([
                '正常' => '正常',
                '空闲' => '空闲',
                '报废' => '报废',
                ])->rules('required')->required();
         
            $form->textarea('remark')->rows(5);
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
